package com.atguigu.gulimall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户购物车 vo类
 * 需要计算的属性，必须重写get方法，保证每次获取属性都会进行计算
 */
public class CartVo {

    /**
     * 商品总数
     */
    private Integer countNum;

    /**
     * 商品类型数量
     */
    private Integer countType;

    /**
     * 选中商品总价
     */
    private BigDecimal totalAmount;

    /**
     * 优惠减免价
     */
    private BigDecimal reducePrice = new BigDecimal("0");

    /**
     * 商品详情
     */
    private List<CartItem> items;

    public Integer getCountNum() {
        int count = 0;
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        int type = 0;
        if (!CollectionUtils.isEmpty(items)) {
            type = items.size();
        }
        return type;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 计算购物项总价
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                if (item.getCheck()) {
                    amount = amount.add(item.getTotalPrice());
                }
            }
        }
        // 减去优惠价格
        BigDecimal subtract = amount.subtract(reducePrice);
        return amount;
    }

    public BigDecimal getReducePrice() {
        return reducePrice;
    }

    public void setReducePrice(BigDecimal reducePrice) {
        this.reducePrice = reducePrice;
    }

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }
}
